package cn.ming.wfchat.app.me.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.material.appbar.AppBarLayout;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;
import butterknife.OnClick;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.dialog.PaymentDialog;
import cn.ming.wfchat.app.me.activity.model.AssetsBean;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.ming.wfchat.kit.utils.Tools;
import cn.wildfirechat.remote.ChatManager;

/**
 * 红包
 */
public class RedPageActivity extends WfcBaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.et_total_amount)
    EditText etTotalAmount;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.et_note)
    EditText etNote;
    @BindView(R.id.tv_total_amount)
    TextView tvTotalAmount;
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.tv_cny)
    TextView tvCny;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    private BigDecimal rete;
    private double balance;
    private String toTarget;


    @Override
    protected void afterViews() {
        super.afterViews();
        toTarget = getIntent().getStringExtra("toTarget");
        initDate();
        setTitle("红包");

    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_red_page;
    }

    void initDate() {
        AppService.Instance().findMyAssets2(new AppService.AssetsCCallback() {
            @Override
            public void onUiSuccess(AssetsBean assetsBean) {
                rete = assetsBean.getList().get(0).getRate();
                balance = assetsBean.getSumAmount().doubleValue();
            }


            @Override
            public void onUiFailure(int code, String msg) {
                ChatManager.showToast(msg);
            }
        });
        etTotalAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String editable = etTotalAmount.getText().toString();
                String regEx = "\\.\\d{6}";  //只能输入字母或数字
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(editable);
                String str = m.replaceFirst("").trim();    //删掉不是字母或数字的字符
                if (!editable.equals(str)) {
                    etTotalAmount.setText(str);  //设置EditText的字符
                    etTotalAmount.setSelection(str.length()); //因为删除了字符，要重写设置新的光标所在位
                    Tools.showToast(RedPageActivity.this, "小数点后输入不能超过5位");
                }
                try {
                    BigDecimal amoutf = new BigDecimal(str);
                    tvCny.setText("≈" + amoutf.multiply(rete) + "CNY");
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                tvTotalAmount.setText(editable);

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        tvNumber.setText("本群人数" + ChatManager.Instance().getGroupMembers(toTarget, false).size() + "人");
    }


    @OnClick(R.id.tv_next)
    void sendRedpage() {
        if (TextUtils.isEmpty(etTotalAmount.getText().toString().trim())) {
            Tools.showToast(RedPageActivity.this, "请输入红包金额");
            return;
        }
        if (TextUtils.isEmpty(etNumber.getText().toString().trim())) {
            Tools.showToast(RedPageActivity.this, "请输入红包数量");
            return;
        }
        int number = 0;
        try {
            number = Integer.valueOf(etNumber.getText().toString().trim());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        if (number < 1 || number > 100) {
            Tools.showToast(RedPageActivity.this, "红包个数不能小于1，大于100");
            return;
        }
        double amount = 0;
        try {
            amount = Double.valueOf(etTotalAmount.getText().toString().trim());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        if (amount > balance) {
            Tools.showToast(RedPageActivity.this, "红包金额不能大于" + balance);
            return;
        }
        PaymentDialog.newInstance("红包", etTotalAmount.getText().toString().trim() + "FJ" + "\n" + tvCny.getText().toString().trim()).setOnClickListener(new PaymentDialog.OnClickListener() {
            @Override
            public void getPwd(String password) {
                AppService.Instance().sendRedpage(etTotalAmount.getText().toString().trim(), etNumber.getText().toString(),
                        etNote.getText().toString(), toTarget, password,
                        new AppService.SendCodeCallback() {
                            @Override
                            public void onUiSuccess() {
                                finish();
                            }

                            @Override
                            public void onUiFailure(String msg) {
                                ChatManager.showToast(msg);
                            }
                        });
            }
        }).show(RedPageActivity.this);

    }

}
